package com.the4man.framework.util.io;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
public class FileUtil {
    private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

    public FileUtil() {
    }

    public void createFolder(String folderPath) {
        File folder = new File(folderPath);
        if(!folder.exists()) {
            folder.mkdir();
        }

    }

    public void deleteFolder(File folder) {
        if(folder.exists()) {
            if(folder.isFile()) {
                folder.delete();
            } else if(folder.isDirectory()) {
                File[] files = folder.listFiles();

                for(int i = 0; i < files.length; ++i) {
                    this.deleteFolder(files[i]);
                }
            }

            folder.delete();
        } else {
            logger.info("所删除的文件不存在！\n");
        }

    }

    public static List<String> getAllFileName(String dirpath) {
        File file = new File(dirpath);
        List<String> fileName = null;
        if(file.isDirectory() && file.exists()) {
            fileName = new ArrayList();
            File[] files = file.listFiles();

            for(int i = 0; i < files.length; ++i) {
                if(files[0].isFile()) {
                    fileName.add(files[i].getName());
                }
            }
        }

        return fileName;
    }
}
